﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopOnline.Models.DAL;
using ShopOnline.Models;
using ShopOnline.Models.Utils;

namespace ShopOnline.Controllers
{
    public class PictureController : Controller
    {
        private static int picturecurent = 0;

        public int Picturecurent
        {
            get { return picturecurent; }
            set { picturecurent = value; }
        }
        private DAL_Picture pictureRepository;
        private ShopOnline.Models.ShopOnlineDBDataContext db;
        private DAL_PictureCatelogy picCateRepository;
        public PictureController()
        {
            pictureRepository = new DAL_Picture();
            db = new Models.ShopOnlineDBDataContext();
            picCateRepository = new DAL_PictureCatelogy();
        }
        //
        // GET: /Picture/
         [Authorize]
        public ActionResult AdminListPicture()
        {
            var list = pictureRepository.getListPicture();
            return View(list);
        }

        //
        // GET: /Picture/Details/5
         [Authorize]
        public ActionResult Details(int id)
        {
            var picture = pictureRepository.getPictureById(id);
            return View(picture);
        }

        //
        // GET: /Picture/Create
         [Authorize]
        public ActionResult Create()
        {
            var listcate = picCateRepository.getListPictureCatelogy();
            ViewData["listcate"] = listcate;
            return View();
        } 

        //
        // POST: /Picture/Create
         [Authorize]
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Picture pic = new Picture();
                pic.body = collection["body"];
                pic.curent = false;
                pic.description = collection["description"];
                pic.title = collection["title"];
                pic.url=collection["url"];
                var show = collection["show"];
                if (show=="false")
                {
                    pic.show = false;
                }
                else
                {
                    pic.show = true;
                }

                #region upload images
                     Uploader uploader = new Uploader();
                     uploader.UploadPath = Server.MapPath("~\\Content\\UploadImages");
                    uploader.IsLowerName = false;
                    uploader.IsEncryptName = false;
                    uploader.IsOverwrite = false;
                    uploader.Prefix = "";
                    uploader.Suffix = "";

                    uploader.AllowedExtensions.Add(".jpg");
                    uploader.AllowedExtensions.Add(".jpeg");
                    uploader.AllowedExtensions.Add(".gif");
                    uploader.AllowedExtensions.Add(".png");
           
                    bool success = uploader.DoUpload("MyFile");
                #endregion
                    pic.imagePath = uploader.PostedFile.FileName; 
                var piccateId = collection["pictureCatelogyId"];
                db.Pictures_add(pic.title, pic.description, pic.body, pic.imagePath, pic.url, int.Parse(piccateId), pic.show, pic.curent);
                return RedirectToAction("AdminListPicture");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Picture/Edit/5
  [Authorize]
        public ActionResult Edit(int id)
        {
            var pic = pictureRepository.getPictureById(id);
            var listcate = picCateRepository.getListPictureCatelogy();
            ViewData["listcate"] = listcate;
            return View(pic);
        }

        //
        // POST: /Picture/Edit/5
 [Authorize]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                Picture pic = pictureRepository.getPictureById(id);
                pic.body = collection["body"];
                pic.curent = false;
                pic.description = collection["description"];
                pic.title = collection["title"];
                pic.url = collection["url"];
                var show = collection["show"];
                if (show == "false")
                {
                    pic.show = false;
                }
                else
                {
                    pic.show = true;
                } 
                var piccateId = collection["pictureCatelogyId"];
                db.Pictures_update(pic.pictureId,pic.title, pic.description, pic.body, pic.imagePath, pic.url, int.Parse(piccateId), pic.show, pic.curent);
                return RedirectToAction("AdminListPicture");
            }
            catch
            {
                var pic = pictureRepository.getPictureById(id);
                var listcate = picCateRepository.getListPictureCatelogy();
                ViewData["listcate"] = listcate;
                return View(pic);
            }
        }

        //
        // GET: /Picture/Delete/5
 [Authorize] 
        public ActionResult Delete(int id)
        {
            var pic = pictureRepository.getPictureById(id);
            return View(pic);
        }

        //
        // POST: /Picture/Delete/5
 [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                var pic = pictureRepository.getPictureById(id);
                pictureRepository.DeletePicture(pic);
                return RedirectToAction("AdminListPicture");
            }
            catch
            {
                var pic = pictureRepository.getPictureById(id);
                return View(pic);
            }
        }
 [Authorize]
        public ActionResult EditImage(int id)
        {
            try
            {
                picturecurent = id;
                var pic = pictureRepository.getPictureById(id);
                return View(pic);
            }
            catch (Exception)
            {
                return RedirectToAction("AdminListPicture");
            }

        }
 [Authorize]
        [HttpPost]
        public ActionResult EditImages()
        {
            try
            {
                var pic = pictureRepository.getPictureById(picturecurent);
                #region upload images
                Uploader uploader = new Uploader();
                uploader.UploadPath = Server.MapPath("~\\Content\\UploadImages");
                uploader.IsLowerName = false;
                uploader.IsEncryptName = false;
                uploader.IsOverwrite = false;
                uploader.Prefix = "";
                uploader.Suffix = "";

                uploader.AllowedExtensions.Add(".jpg");
                uploader.AllowedExtensions.Add(".jpeg");
                uploader.AllowedExtensions.Add(".gif");
                uploader.AllowedExtensions.Add(".png");

                bool success = uploader.DoUpload("MyFile");
                #endregion
                string abc = uploader.PostedFile.FileName;
                db.Pictures_update(pic.pictureId, pic.title, pic.description, pic.body, abc, pic.url, pic.pictureCatelogyId, pic.show, pic.curent);
                return RedirectToAction("Edit", new { id = picturecurent });
            }
            catch (Exception)
            {
                return RedirectToAction("EditImage", new { id= picturecurent});
            }
           
        }
    }
}
